var common = require('../../utils/common.js');
const app = getApp()
Page({

  data: {
    shopInfo: [],
    regboxShow: false
  },
  onLoad: function (options) {
    const that = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('acceptDataFromOpenerPage', function(data) {
      console.log(data)
      that.setData({
        shopInfo: data.shopInfo
      })
    })
  },
  switch1Change(e){
    console.log(e)
    if(e.detail.value){
      this.auto()
    }else{
      this.manual()
    }
  },
  auto() {
    const that = this
    common.post({
      url: '/app/v2/shop/updateMyShop', data: { sessionId: app.globalData.sessionId, shopName: this.data.shopInfo.shopName, isAutoChoose: true }
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == "0") {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          })
        } else if (res.data.code == "-9998") {
          that.loginTip()
        } else {
          console.log(res)
        }
      }).catch((res) => {
        console.log(res)
      })
  },
  manual() {
    const that = this
    common.post({
      url: '/app/v2/shop/updateMyShop', data: { sessionId: app.globalData.sessionId, shopName: this.data.shopInfo.shopName, isAutoChoose: false }
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == "0") {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          })
        } else if (res.data.code == "-9998") {
          that.loginTip()
        } else {
          console.log(res)
        }
      }).catch((res) => {
        console.log(res)
      })
  },
  modifyName(){
    this.setData({
      regboxShow: true
    })
  },
  modifyShop(){
    const that = this
    common.post({
      url: '/app/v2/shop/updateMyShop', data: { sessionId: app.globalData.sessionId, shopName: this.data.storeName }
    })
      .then((res) => {
        console.log(res.data)
        if (res.data.code == "0") {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 2000
          })
          that.data.shopInfo.shopName = this.data.storeName
          that.setData({
            regboxShow: false,
            shopName: that.shopInfo.shopName
          })

        } else if (res.data.code == "-9998") {
          that.loginTip()
        } else {
          console.log(res)
        }
      }).catch((res) => {
        console.log(res)
      })
  },
  inputWacth: function (e) {
    console.log(e);
    this.setData({
      storeName: e.detail.value
    });
  },
  closeRegbox(){
    this.setData({
      regboxShow: false,
      storeName: ''
    })
  }
})